//一个小的reducer就代表一个小的子store,需要传递两个参数：state he action,reducer必须是一个纯函数：不能直接修改入参，相同的入参永远返回相同结果
//操作流程固定的：1.当信号到达时，先对STATE进行深copy,根据action 中的type  payload对老的state进行操作；返回修改后的那个中最新的state

import produce from 'immer'

const initState = {
    size: localStorage.getItem('size') || 'middle',
    lang: localStorage.getItem('lang') || navigator.language.split('-')[0],
    color: localStorage.getItem('color') || '#1890ff'


}

//数据持久化  不会随刷新失效
function reducer(state = initState, { type, payload }) {

    return produce(state, (newState) => {
        switch (type) {
            case 'APP_SIZE':
                newState.size = payload
                localStorage.setItem('size', payload)
                break;

            case 'APP_LANG':
                newState.lang = payload
                localStorage.setItem('lang', payload)
                break;

            case 'APP_COLOR':
                newState.color = payload
                localStorage.setItem('color', payload)
                break;

            default:
        }
    })
}

export default reducer;